package com.qianniu.napi.admin.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;

import com.qianniu.napi.admin.entity.Saccount;
import com.qianniu.napi.admin.entity.JsonDO;
import com.qianniu.napi.admin.entity.Jinfo;
import com.qianniu.napi.admin.entity.User;
import com.qianniu.napi.admin.service.ISaccountService;

import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.helper.HttpHelper;
import com.qianniu.napi.common.util.EncryptUtil;
import com.qianniu.napi.common.util.ObjectUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>
 * 商家账务表
 * </p>
 *
 * @author chenxuebin
 * @since 2020-07-22
 */
@Controller
public class SaccountController {

    private static final Logger logger = LoggerFactory.getLogger(SaccountController.class);

    @Autowired
    private ISaccountService saccountService;

    @RequestMapping("admin/saccount/index")
    public String index() {
        return "admin/saccount";
    }

    @RequestMapping("/admin/saccount/list")
    @ResponseBody
    public Page<Saccount> list(@RequestParam(value="limit",required = false) int limit,
                                @RequestParam(value="offset",required = false) int offset) {
        Wrapper<Saccount> wrapper = new EntityWrapper<Saccount>().eq("mpid",ObjectUtil.getMpid());
        wrapper.orderBy("id",true);
        Page<Saccount> page = saccountService.selectPage(new Page<Saccount>(offset/limit+1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/saccount/add")
    public String add(@Valid Saccount saccount) {

        saccount.setMid(ObjectUtil.getMid());
        saccount.setMpid(ObjectUtil.getMpid());
        saccount.setCreatetime(new Date());

        saccount.insert();
        return "admin/saccount";
    }

    @RequestMapping("/admin/saccount/getSaccountById")
    @ResponseBody
    public Saccount getSaccountById(long id) {
        Saccount saccount = saccountService.selectById(id);
        return saccount;
    }

    @RequestMapping("/admin/saccount/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = saccountService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/saccount/changeState")
    @ResponseBody
    public String changeState(long sid, int state) {
        Saccount saccount = new Saccount();
        saccount.setSid(sid);
        saccount.setState(state);
        saccountService.updateById(saccount);
        return "success";
    }

    @RequestMapping("/admin/saccount/modify")
    public String update(Saccount saccount) {
        boolean isSuccc = saccountService.updateById(saccount);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

    /// / ************* bellow is saccount interface for user *********

    @RequestMapping("/api/saccount/list")
    @ResponseBody
    public Jinfo list(@RequestParam(value="status",required = false)String status,
                       @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                HttpServletRequest req) {
        Jinfo info = new Jinfo();
        if(pageSize==null)pageSize = 10;
        Page page=new Page(pageIndex,pageSize);
        page = saccountService.selectPage(page, new EntityWrapper<Saccount>()
                .eq("uid", ObjectUtil.getUid())
                .eq("state", 1));
        info.setInfo(page.getRecords());
        return info;
    }

    @RequestMapping("/api/saccount/add")
    @ResponseBody
    public Jinfo saccountAdd(@Valid Saccount saccount,
                          BindingResult bindingResult,HttpServletRequest req) {
        Jinfo info = new Jinfo();
        // 字段校验
        if(bindingResult.hasErrors())
        {
            logger.debug( "There are errors! {}", bindingResult );
            info.setMsg("There are errors! "+bindingResult);
            info.setCode(400);
            return info;
        }
        saccount.setCreatetime(new Date());
        saccount.insert();
        return info;
    }

    @RequestMapping("/api/saccount/modify")
    @ResponseBody
    public Jinfo saccountUpdate(HttpServletRequest req,
                                    Saccount saccount) {
        Jinfo info = new Jinfo();
        saccount.setModifytime(new Date());
        saccount.updateById();
        return info;
    }

    @RequestMapping("/api/saccount/detail")
    @ResponseBody
    public Jinfo getSaccountDetailById(@RequestParam("token") String token,
                                       @RequestParam("id") long id,HttpServletRequest req
    ) {
        Jinfo info = new Jinfo();
        Saccount saccount = saccountService.selectById(id);
        info.setInfo(saccount);
        return info;
    }
}
